package com.huopaolan.yohuoshop.Mvp.View.Activity;

import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.huopaolan.lib_core.Base.BaseActivity;
import com.huopaolan.yohuoshop.Bean.UserRegisterEntity;
import com.huopaolan.yohuoshop.Dao.Service.DBService;
import com.huopaolan.yohuoshop.Di.Compont.DaggerRegisterCompont;
import com.huopaolan.yohuoshop.Di.Modules.RegisterModules;
import com.huopaolan.yohuoshop.Mvp.Contract.UserContract;
import com.huopaolan.yohuoshop.Mvp.Presenter.RegisterPresenter;
import com.huopaolan.yohuoshop.R;
import com.huopaolan.yohuoshop.Sp.SpUtils;

import org.greenrobot.eventbus.EventBus;

public class RegisterActivity extends BaseActivity<RegisterPresenter> implements UserContract.RegisterIView, OnClickListener {
    private ImageView back;
    private ImageView cancel;
    private EditText register_phone, register_code, register_password;
    private Button register_button;

    @Override
    public int layoutID() {
        return R.layout.activity_user_register;
    }

    @Override
    public void initView() {
        //初始化
        back = findViewById(R.id.iv_register_back);
        cancel = findViewById(R.id.iv_register_clear);
        register_phone = findViewById(R.id.et_register_phone_zh);
        register_code = findViewById(R.id.et_register_phone_yzm);
        register_password = findViewById(R.id.et_register_y_pawd);
        register_button = findViewById(R.id.btn_register_register);
        back.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        Window window = getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void initData() {
        register_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String register_name = register_phone.getText().toString();
                String register_pass = register_password.getText().toString();
                if (TextUtils.isEmpty(register_name)) {
                    Toast.makeText(RegisterActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(register_pass)) {
                    Toast.makeText(RegisterActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                p.RegisterRequest(register_name, register_pass);
            }
        });
    }

    @Override
    public void setUpDagger() {
        DaggerRegisterCompont.builder()
                .registerModules(new RegisterModules(this))
                .build().inject(this);
    }

    @Override
    public void RegisterSuccess(UserRegisterEntity entity) {
        Toast.makeText(this, "" + entity.values.get(0).nick_name, Toast.LENGTH_SHORT).show();
        if (entity == null) {
            Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
            return;
        }
        DBService.getDbService().delete("User", null, null);
        //存入数据库
        ContentValues values = new ContentValues();
        values.put("user_id", entity.values.get(0).user_id);
        values.put("nick_name", entity.values.get(0).nick_name);
        values.put("user_name", entity.values.get(0).user_name);
        values.put("user_pwd", entity.values.get(0).user_pwd);
        long user = DBService.getDbService().insert("User", values);
        if (user > 0) {
            Log.d("DBService", "数据库储存成功");
            DBService.getDbService().close();
            EventBus.getDefault().post(entity.values.get(0).user_id);
            SpUtils.getInstance().SpWrite(true);
            Toast.makeText(this, "" + entity.msg + "\n" + "注册成功", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Log.d("DBService", "数据库储存失败");
        }
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_register_back:
                startActivity(new Intent(this, LoginActivity.class));
                finish();
                break;
            case R.id.iv_register_clear:
                finish();
                break;
        }
    }
}
